package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 预约视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "预约视图对象", description = "预约信息的视图对象")
public class ReservationVO {

    @Schema(description = "预约ID")
    private String id;

    @Schema(description = "预约用户ID")
    private String userId;

    @Schema(description = "预约用户姓名")
    private String userName;

    @Schema(description = "用户学号/工号")
    private String userNumber;

    @Schema(description = "图书ID")
    private String bookId;

    @Schema(description = "图书标题")
    private String bookTitle;

    @Schema(description = "图书ISBN")
    private String bookIsbn;

    @Schema(description = "图书作者")
    private String bookAuthor;

    @Schema(description = "图书封面")
    private String bookCover;

    @Schema(description = "图书副本ID")
    private String bookCopyId;

    @Schema(description = "条形码")
    private String barcode;

    @Schema(description = "预约日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate reservationDate;

    @Schema(description = "预约到期日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate expirationDate;

    @Schema(description = "可取书日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate availableDate;

    @Schema(description = "取书日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate pickupDate;

    @Schema(description = "预约状态")
    private Integer status;

    @Schema(description = "预约状态名称")
    private String statusName;

    @Schema(description = "排队位置")
    private Integer queuePosition;

    @Schema(description = "预约优先级")
    private Integer priority;

    @Schema(description = "通知方式")
    private Integer notificationMethod;

    @Schema(description = "通知方式名称")
    private String notificationMethodName;

    @Schema(description = "是否已通知")
    private Integer notified;

    @Schema(description = "通知时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime notificationTime;

    @Schema(description = "取消原因")
    private String cancellationReason;

    @Schema(description = "取消时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime cancellationTime;

    @Schema(description = "操作员ID")
    private String operatorId;

    @Schema(description = "操作员姓名")
    private String operatorName;

    @Schema(description = "备注")
    private String remarks;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取预约状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "预约中";
            case 1 -> "可取书";
            case 2 -> "已取书";
            case 3 -> "已取消";
            case 4 -> "已过期";
            default -> "未知";
        };
    }

    /**
     * 获取通知方式名称
     */
    public String getNotificationMethodName() {
        if (notificationMethod == null) {
            return "未设置";
        }
        return switch (notificationMethod) {
            case 1 -> "邮件";
            case 2 -> "短信";
            case 3 -> "站内信";
            default -> "未知";
        };
    }

    /**
     * 判断是否已过期
     */
    public boolean isExpired() {
        return expirationDate != null && LocalDate.now().isAfter(expirationDate);
    }

    /**
     * 判断是否可取书
     */
    public boolean isAvailableForPickup() {
        return status != null && status == 1 && 
               availableDate != null && 
               !LocalDate.now().isBefore(availableDate) &&
               !isExpired();
    }

    /**
     * 计算剩余天数
     */
    public int getRemainingDays() {
        if (expirationDate == null) {
            return 0;
        }
        return (int) java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(), expirationDate);
    }
}
